<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="" prop="purchaseOrderNumber">
        <el-input
          v-model="queryParams.purchaseOrderNumber"
          placeholder="请输入采购单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="planGenerationTime">
        <!-- <el-date-picker
          clearable
          v-model="queryParams.planGenerationTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择计划生成时间"
        >
        </el-date-picker> -->
        <el-date-picker
          clearable
          v-model="queryParams.planGenerationTimeStart"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="计划生成开始日期"
        >
        </el-date-picker>
        <el-date-picker
          clearable
          v-model="queryParams.planGenerationTimeEnd"
          type="date"
          value-format="yyyy-MM-dd 23:59:59"
          placeholder="计划生成结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="plannedAllocationQty">
        <el-input
          v-model="queryParams.plannedAllocationQty"
          placeholder="请输入计划分配数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="actualAllocationQty">
        <el-input
          v-model="queryParams.actualAllocationQty"
          placeholder="请输入实际分配数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="allocationTime">
        <!-- <el-date-picker
          clearable
          v-model="queryParams.allocationTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择分配时间"
        >
        </el-date-picker> -->
        <el-date-picker
          clearable
          v-model="queryParams.allocationTimeStart"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="分配开始日期"
        >
        </el-date-picker>
        <el-date-picker
          clearable
          v-model="queryParams.allocationTimeEnd"
          type="date"
          value-format="yyyy-MM-dd 23:59:59"
          placeholder="分配结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="allocationFailureReason">
        <el-input
          v-model="queryParams.allocationFailureReason"
          placeholder="请输入分配日志"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="warehouseDocumentNumber">
        <el-input
          v-model="queryParams.warehouseDocumentNumber"
          placeholder="请输入进出仓单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="dataStatus">
        <el-select
          v-model="queryParams.dataStatus"
          placeholder="数据状态"
          clearable
        >
          <el-option
            v-for="dict in typeArr"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="productType">
        <!-- <el-input v-model="form.fieldName" placeholder="请输入字段名" /> -->
        <el-select
          v-model="queryParams.productType"
          placeholder="商品类型"
          clearable
        >
          <el-option label="良品" value="Good" />
          <el-option label="瑕疵" value="Defective" />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="allocationStatus">
        <el-select
          v-model="queryParams.allocationStatus"
          placeholder="分配状态"
          clearable
        >
          <el-option label="分配成功" value="allocateSuccess" />
          <el-option label="分配失败" value="allocateFailure" />

          <el-option label="待分配" value="toAllocate" />

          <!-- <el-option
            v-for="dict in dict.type.sys_product_allocation_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          /> -->
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="分配方案ID">
        <el-input
          v-model="queryParams.taskId"
          placeholder="请输入分配方案ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['productAllocation:BiInventoryAllocation:add']"
          >新增</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['productAllocation:BiInventoryAllocation:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['productAllocation:BiInventoryAllocation:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['productAllocation:BiInventoryAllocation:export']"
          >导出</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleexecuteBatchAllocateInventory"
          >调配库存</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          @click="handlecancelBatchAllocateInventory"
          >取消调配</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain size="mini" @click="handlereExecuteFailure"
          >失败重分配</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="BiInventoryAllocationList"
      @selection-change="handleSelectionChange"
      :height="500"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" width="80" />
      <el-table-column
        label="店铺名称"
        align="center"
        prop="storeName"
        width="120"
      />
      <el-table-column label="库存来源" align="center" prop="inventorySource" />
      <!-- <el-table-column
        label="采购单号"
        align="center"
        prop="purchaseOrderNumber"
      /> -->
      <el-table-column label="商品类型" align="center" prop="productType">
        <template slot-scope="scope">
          <div>
            {{ scope.row.productType | state1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="数据状态" align="center" prop="dataStatus"
        ><template slot-scope="scope">
          <div>
            {{ scope.row.dataStatus | state3 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="商品编码"
        align="center"
        prop="productCode"
        width="140"
      />

      <el-table-column label="分配方案ID" align="center" prop="taskId" />
      <el-table-column
        label="计划生成时间"
        align="center"
        prop="planGenerationTime"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="计划分配数量"
        align="center"
        prop="plannedAllocationQty"
      />
      <el-table-column
        label="实际分配数量"
        align="center"
        prop="actualAllocationQty"
      />
      <el-table-column
        label="分配时间"
        align="center"
        prop="allocationTime"
        width="180"
      >
      </el-table-column>
      <el-table-column label="分配状态" align="center" prop="allocationStatus"
        ><template slot-scope="scope">
          <div>
            {{ scope.row.allocationStatus | state2 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="分配日志"
        align="center"
        prop="allocationFailureReason"
      >
        <template slot-scope="scope">
          <mypopover
            :popoverInput="scope.row.allocationFailureReason"
          /> </template
      ></el-table-column>
      <el-table-column
        label="进出仓单号"
        align="center"
        prop="warehouseDocumentNumber"
      />
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['productAllocation:BiInventoryAllocation:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['productAllocation:BiInventoryAllocation:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存分配明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="店铺名称" prop="storeName">
          <!-- <el-input
            :disabled="true"
            v-model="form.storeName"
            placeholder="请输入店铺名称"
          /> -->
          <el-select
            :disabled="form.dataStatus != 'abnormal'"
            v-model="form.storeName"
            clearable
            filterable
            placeholder="店铺"
            style="width: 200px"
          >
            <el-option
              v-for="item in ShopInfoList"
              :key="item.comment"
              :label="`${item.comment}`"
              :value="item.comment"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="采购单号" prop="purchaseOrderNumber">
          <el-input
            :disabled="true"
            v-model="form.purchaseOrderNumber"
            placeholder="请输入采购单号"
          />
        </el-form-item> -->
        <el-form-item label="商品编码" prop="productCode">
          <el-input
            :disabled="true"
            v-model="form.productCode"
            placeholder="请输入商品编码"
          />
        </el-form-item>
        <el-form-item label="计划生成时间" prop="planGenerationTime">
          <el-date-picker
            clearable
            :disabled="true"
            v-model="form.planGenerationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划生成时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划分配数量" prop="plannedAllocationQty">
          <el-input
            v-model="form.plannedAllocationQty"
            placeholder="请输入计划分配数量"
          />
        </el-form-item>
        <el-form-item label="实际分配数量" prop="actualAllocationQty">
          <el-input
            :disabled="true"
            v-model="form.actualAllocationQty"
            placeholder="请输入实际分配数量"
          />
        </el-form-item>
        <el-form-item label="分配时间" prop="allocationTime">
          <el-date-picker
            :disabled="true"
            clearable
            v-model="form.allocationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择分配时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="分配日志" prop="allocationFailureReason">
          <el-input
            :disabled="true"
            v-model="form.allocationFailureReason"
            placeholder="请输入分配日志"
          />
        </el-form-item>
        <el-form-item label="进出仓单号" prop="warehouseDocumentNumber">
          <el-input
            :disabled="true"
            v-model="form.warehouseDocumentNumber"
            placeholder="请输入进出仓单号"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiInventoryAllocation,
  getBiInventoryAllocation,
  delBiInventoryAllocation,
  addBiInventoryAllocation,
  updateBiInventoryAllocation,
  executeBatchAllocateInventory,
  cancelBatchAllocateInventory,
  reExecuteFailure,
} from "@/api/productAllocation/BiInventoryAllocation";
import { JstShopInfo } from "@/api/conf/BiStoreWarehouseBinding";
export default {
  name: "BiInventoryAllocation",
  dicts: ["sys_product_allocation_status", "sys_product_datastatus"],

  props: {
    abnormalAllocPlanId: {
      type: Number,
      default: null,
    },
  },
  data() {
    return {
      typeArr: [
        { value: "normal", label: "正常" },
        { value: "abnormal", label: "异常重分配" },
        // { value: "近1年", label: "近1年" },
      ],
      ShopInfoList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存分配明细表格数据
      BiInventoryAllocationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeName: null,
        purchaseOrderNumber: null,
        productType: null,
        productCode: null,
        planGenerationTime: null,
        plannedAllocationQty: null,
        actualAllocationQty: null,
        allocationTime: null,
        allocationStatus: null,
        allocationFailureReason: null,
        warehouseDocumentNumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storeName: [
          { required: true, message: "店铺名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  filters: {
    state3(string) {
      if (string == "normal") {
        return "正常";
      }
      if (string == "abnormal") {
        return "异常重分配";
      }
    },
    state2(string) {
      if (string == "allocateSuccess") {
        return "分配成功";
      }
      if (string == "allocateFailure") {
        return "分配失败";
      }
      if (string == "toAllocate") {
        return "待分配";
      }
      if (string == "isAllocated") {
        return "已分配";
      }
    },
    state1(string) {
      if (string == "Good") {
        return "良品";
      }
      if (string == "Defective") {
        return "瑕疵";
      }
    },
  },
  created() {
    this.getList();
    JstShopInfo({ pageNum: 1, pageSize: 100 }).then((response) => {
      this.ShopInfoList = response.rows.map((item) => ({
        columnName: item.shopId,
        comment: item.shopName,
      }));
    });
  },
  watch: {
    abnormalAllocPlanId: {
      handler: function (newnew, oldold) {
        if (this.abnormalAllocPlanId) {
          this.queryParams.taskId = this.abnormalAllocPlanId;
          this.getList();
        }
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    handlereExecuteFailure() {
      this.openLoading();
      reExecuteFailure().then((response) => {
        this.$modal.closeLoading();
        if (response.code == 200) {
          this.$modal.msgSuccess("已重新分配");
        }
        this.getList();
      });
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在加载，请稍候！");
    },

    handlecancelBatchAllocateInventory() {
      if (this.ids.length < 1) {
        this.$modal.msgError("请至少选择一条数据");
        return;
      }
      this.openLoading();
      cancelBatchAllocateInventory(this.ids).then((response) => {
        this.$modal.closeLoading();
        if (response.code == 200) {
          this.$modal.msgSuccess("已取消调配");
          this.getList();
        }
      });
    },
    handleexecuteBatchAllocateInventory() {
      if (this.ids.length < 1) {
        this.$modal.msgError("请至少选择一条数据");
        return;
      }
      this.openLoading();
      executeBatchAllocateInventory(this.ids).then((response) => {
        this.$modal.closeLoading();
        if (response.code == 200) {
          this.$modal.msgSuccess("调配库存成功");
          this.getList();
        }
        if (response.code == 500) {
          this.$modal.msgError("已调配，不能在调配");
          this.getList();
        }
      });
    },
    /** 查询库存分配明细列表 */
    getList() {
      this.loading = true;
      listBiInventoryAllocation(this.queryParams).then((response) => {
        this.BiInventoryAllocationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeName: null,
        purchaseOrderNumber: null,
        productType: null,
        productCode: null,
        planGenerationTime: null,
        plannedAllocationQty: null,
        actualAllocationQty: null,
        allocationTime: null,
        allocationStatus: null,
        allocationFailureReason: null,
        warehouseDocumentNumber: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {};
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存分配明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiInventoryAllocation(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存分配明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiInventoryAllocation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiInventoryAllocation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除库存分配明细编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiInventoryAllocation(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "productAllocation/BiInventoryAllocation/export",
        {
          ...this.queryParams,
        },
        `BiInventoryAllocation_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
